package com.common.baselib.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * -----------
 */
public class AppUtils {

    @SuppressLint("StaticFieldLeak")
    private static Context context;


    private static boolean isInitSplash = false;


    /**
     * 初始化工具类
     *
     * @param app 应用
     */
    public static void init(@NonNull final Application app) {
        AppUtils.context = app.getApplicationContext();
    }

    /**
     * 初始化工具类
     *
     * @param context 应用
     */
    public static void init(@NonNull final Context context) {
        AppUtils.context = context;
    }

    /**
     * 获取Application
     *
     * @return Application
     */
    @NonNull
    public static Context getContext() {
        if (context != null) {
            return AppUtils.context;
        }
        return null;
    }
    public static boolean isContextValid(Context context) {
        if (context == null) return false;

        Activity activity = findActivity(context);
        if (activity == null) return false;

        return !activity.isDestroyed() && !activity.isFinishing();
    }
    @Nullable
    public static Activity findActivity(Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
}